<%@ page language="java" contentType="text/html; charset=US-ASCII"
    pageEncoding="US-ASCII"%>
<%@ page import="structures.User"%>
<%@ page import="structures.AD"%>
<%User user = (User) session.getAttribute("user");%>
<%	AD advertisement = (AD) session.getAttribute("advertisement");
	String feedbackError = (String) session.getAttribute("feedback");
	String gamifyErrors = (String) session.getAttribute("gamifyErrors");
	session.removeAttribute("feedback");
	session.removeAttribute("gamifyErrors");
	session.removeAttribute("advertisement");
	String imageSource = "http://localhost:8080/DatabaseRESTWebService/GetADImage?idAdvertisement=";%>
	
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>TiVendoIo - Item</title>

    <!-- Bootstrap -->
    <link href="/serviceTechnologies/bootstrap/css/bootstrap.min.css" rel="stylesheet">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
      <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    
    <link href="//netdna.bootstrapcdn.com/font-awesome/4.0.0/css/font-awesome.css" rel="stylesheet">
    <link href="css/style.css" rel = "stylesheet">
    
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script src="/serviceTechnologies/bootstrap/js/bootstrap.js"></script>
    
    <script type="text/javascript">
	$(document).ready(function(){
		$('#footer').load('/serviceTechnologies/common/footer.jsp');
		$('#navbar').load('/serviceTechnologies/common/navbar.jsp');
	});
	</script>
	
  </head>
  <body>
 	
 	<div id="navbar"></div>
 	
   <div class="container">
   <div class = "row">
  		<div class = "col-md-6">
   		<h3><%=advertisement.getTitle()%></h3>
   		</div>
   		
   		<div class = "col-md-1 pull-right">
   		<h3><%=advertisement.getPrice()%> &euro;</h3>
   		</div>
   </div>
   
   <br>
   
   <div class = "row">
   		<div class = "col-md-6">
   			<img src="<%=imageSource + advertisement.getIdAdvertisement()%>" id="Image1" alt="" class = "img-rounded center-block">
   		</div>
   		<div class = "col-md-6">
   			<div class = "row">
   				<div class = "col-md-12">
   					<p align = "justify"><%=advertisement.getDescription()%></p>
   				</div>
   			</div>
   			<div class = "row">
   				<div class = "col-md-6">
   					<a href="./ShowUser?idUser=<%=advertisement.getIdUser()%>&idAdvertisement=<%=advertisement.getIdAdvertisement()%>">
						<button class="btn btn-default btn-primary btn-sm btn-block"><span class="glyphicon glyphicon-user"></span> Seller</button>
  					</a>
   				</div>
   				<div class = "col-md-6">
   					<%if(advertisement.getBoughtBy()!=null){
						if(advertisement.getFeedbackText()!=null){%>
  							<p><span class="glyphicon glyphicon-ok-circle"></span> Already Bought!</p>
  							<p>Feedback text: <%=advertisement.getFeedbackText()%></p>
  							<p>Feedback stars: <%=advertisement.getFeedbackValue()%></p>
  						<%}else{
  								if(Integer.parseInt(advertisement.getBoughtBy())==user.getId()){%>
  										
  										
  										<button class="btn btn-default btn-primary btn-sm btn-block" data-toggle="modal" data-target="#myModal">
										  <span class="glyphicon glyphicon-pencil"></span>Leave a Feedback
										</button>
  										
  										<div class="modal fade" id="myModal">
										  <div class="modal-dialog">
										    <div class="modal-content">
										      <div class="modal-header">
										        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
										        <h4 class="modal-title">Leave a Feedback</h4>
										      </div>
										      <div class="modal-body">
										        <form action="./LeaveFeedback" autocomplete="off" method="POST">
		  											<div class="form-group">
		  												<label for="inputFeed">Feedback:</label>
		  												<textarea class="form-control" id="inputFeed" rows="3" name = "Text" placeholder="Here the comment"></textarea>
		  											</div>
		  											<div class="form-group">
			  											<select name="Value">
			  												<option value="true">Suggest!</option>
			  												<option value="false">No Suggest!</option>
			  											</select>
		  											</div>
		  											<input type="hidden" name="idSeller" value="<%=advertisement.getIdUser()%>">
		  											<input type="hidden" name="idAdvertisement" value="<%=advertisement.getIdAdvertisement()%>">
		  											<!-- Here the button -->
		  											<div style = "text-align:center;">
		  											<button class="btn  btn-primary" type="submit">Leave a feedback</button>
		  											</div>
										        </form>
										      </div>
										      
										    </div><!-- /.modal-content -->
										  </div><!-- /.modal-dialog -->
										</div><!-- /.modal -->
  										
  										
  										
	  							<%}else{%>
	  									<p><span class="glyphicon glyphicon-ok-circle"></span> Already Bought!</p>
	  							<%}%>
  						  <%}%>
  				    <%}else{%>
						<form action="http://localhost:8080/ISellToYouRESTWebService/StartPayPalPayment">
							<button class="btn btn-default btn-primary btn-sm btn-block" ><span class="glyphicon glyphicon-usd"></span> Pay with Paypal</button>
							<input type="hidden" name="emailOwner" value="<%=advertisement.getPayPalEmail()%>">
							<input type="hidden" name="idUser" value = "<%=user.getId()%>">
							<input type="hidden" name="cost" value="<%=advertisement.getPrice()%>">
							<input type="hidden" name="idAdvertisement" value="<%=advertisement.getIdAdvertisement()%>">
							<input type="hidden" name="returnUrl" value="http://127.0.0.1:8080/serviceTechnologies/PayPalReturn">
						</form>
					<%}%>
   				</div>  <!-- End Paypal div -->
   			</div> <!-- End Button row -->
   		</div>
   </div>
   <br>
   <div class = "row">
   		<%if(advertisement.getWikiInfo()!=""){%>
   			<blockquote>
			  <p align = "justify" style = "font-size:10px"><%=advertisement.getWikiInfo()%></p>
			  <footer>Wikipedia</footer>
			</blockquote> 
  				<%  }%>
   </div>
	<br>
	
				
  				
  				
	
  	<br><br>
				
    </div> <!-- End Container -->
 	
 	<div id="footer">
      
    </div>
 	<!-- End footer -->
 	<%if(feedbackError!=null){ %>
	 	<%if(feedbackError.equals("false")){ %>
	 		<script type="text/javascript">alert("Feedback inserted")</script>
	 	<%}else{%>
	 		<script type="text/javascript">alert("Feedback NOT inserted")</script>
	 	<%}%>
 	<%}%>
 	
 	<%if(gamifyErrors!=null){ %>
	 	<%if(gamifyErrors.equals("no")){ %>
	 		<script type="text/javascript">alert("Gamify ok")</script>
	 	<%}else{%>
	 		<script type="text/javascript">alert("Gamify error")</script>
	 	<%}%>
 	<%}%>
 	
  </body>
 </html>